/* Copyright (c) 2002-2009 Grupo e-Gen.  All rights reserved.
 *
 * ====================================================================
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * either version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 * 
 */

package conultoria.testes.exercicio;

import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import conultoria.testes.Proprietary;

/**
 * Class: TesteUsoSWT
 *   
 * @author Grupo e-Gen 
 * @version 4.1 (24/06/2009)
 * @since 4.1 (24/06/2009)
 */
public class TesteUsoSWT {
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Teste");

		GridLayout gdLayout = new GridLayout();
		shell.setLayout(gdLayout);


		ListViewer listViewer = new ListViewer(shell, SWT.SINGLE);
		GridData gdData = new GridData();
		gdData.horizontalAlignment = SWT.FILL;
		gdData.verticalAlignment = SWT.FILL;
		gdData.grabExcessVerticalSpace = true;
		gdData.grabExcessHorizontalSpace = true;
		listViewer.getList().setLayoutData(gdData);


		Veiculo[] veic = new Veiculo[5];
		for (int i = 0; i<veic.length; i++) {
			veic[i] = new Veiculo();
			veic[i].setName("Veiculo"+i);
			veic[i].setTipo("Tipo"+i);
			veic[i].setPeso(i);
			veic[i].setColor(100, 100, 100);
		}

		listViewer.setContentProvider(new IStructuredContentProvider() {
			@Override
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			}

			@Override
			public void dispose() {
			}

			@Override
			public Object[] getElements(Object inputElement) {
				return (Object[])inputElement;
			}
		});

		/*LabelProvider
		 * 
		 * INICIO
		 * */

		ILabelProvider lProvider = new ILabelProvider() {

			@Override
			public void removeListener(ILabelProviderListener listener) {
			}

			@Override
			public boolean isLabelProperty(Object element, String property) {
				return false;
			}

			@Override
			public void dispose() {
			}

			@Override
			public void addListener(ILabelProviderListener listener) {
			}

			@Override
			public String getText(Object element) {
				if (element instanceof Veiculo) {
					Veiculo veic = (Veiculo) element;
					return veic.getName();
				}
				if (element instanceof Proprietary) {
					Proprietary prop = (Proprietary) element;
					return prop.getName();
				}
				return null;
			}

			@Override
			public Image getImage(Object element) {
				return null;
			}
		};

		listViewer.setLabelProvider(lProvider);

		listViewer.setInput(veic);
		listViewer.add(veic);

		/*FIM*/


		TableViewer  tableV = new TableViewer(shell, SWT.FULL_SELECTION);

		TableViewerColumn tvc1 = new TableViewerColumn(tableV, SWT.NONE);

		tableV.getTable().setHeaderVisible(true);
		
		tableV.setContentProvider(new IStructuredContentProvider() {

			@Override
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
				//				System.out.println("input changed");
			}

			@Override
			public void dispose() {
				// TODO Auto-generated method stub

			}

			@Override
			public Object[] getElements(Object inputElement) {
				//				System.out.println("Preparando Estrutura de Items");
				return (Object[]) inputElement;
			}
		});
		tableV.setLabelProvider(new ITableLabelProvider(){

			@Override
			public void addListener(ILabelProviderListener listener) {
				// TODO Auto-generated method stub

			}

			@Override
			public void dispose() {
				// TODO Auto-generated method stub

			}

			@Override
			public boolean isLabelProperty(Object element, String property) {
				System.out.println(element+"---->"+property);
				return false;
			}

			@Override
			public void removeListener(ILabelProviderListener listener) {
				// TODO Auto-generated method stub

			}

			@Override
			public Image getColumnImage(Object element, int columnIndex) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public String getColumnText(Object element, int columnIndex) {
				if (element instanceof Veiculo) {
					Veiculo veic = (Veiculo) element;
					switch(columnIndex) {
						case 0: return veic.getName(); 
						case 1: return veic.getTipo(); 
						case 2: return String.valueOf(veic.getPeso());
						case 3: return veic.getColor()[0]+","+veic.getColor()[1]+","+veic.getColor()[2];
					}
				}
				return null;
			}

		});

		tableV.setInput(veic);
		tableV.getTable().setLayoutData(gdData);

		//dimensionar
		TableLayout tableLayout = new TableLayout();
		tableLayout.addColumnData(new ColumnWeightData(30, 100,true));
		tableLayout.addColumnData(new ColumnWeightData(30, 100,true));
		tableLayout.addColumnData(new ColumnWeightData(20, 100,true));
		tableLayout.addColumnData(new ColumnWeightData(20, 100,true));
		tableV.getTable().setLayout(tableLayout);


		shell.open();
		while(!display.isDisposed()){
			if(!display.readAndDispatch()){
				display.sleep();
			}
		}


	}

}
